﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace Abhiruchi_Phase1
{
    /// <summary>
    /// Interaction logic for TaskManagement.xaml
    /// </summary>
    public partial class ORG_TaskMaster : Window
    {
        public static string Mode = "";

        public ORG_TaskMaster()
        {
            InitializeComponent();

            #region Load Basic Details
            DataTable mydtFrCmbSection = ORGTaskMasterDAL.GetDBDetails(6);

            GetTaskIdentity();

            if (mydtFrCmbSection.Rows.Count > 0)
            {
                cmbShowSec.DataContext = mydtFrCmbSection.DefaultView;
                cmbShowSec.DisplayMemberPath = mydtFrCmbSection.Columns["SectionName"].ToString();
                cmbShowSec.SelectedValuePath = mydtFrCmbSection.Columns["SectionId"].ToString();
            }
            cmbShowGen.Items.Add("Select");
            cmbShowGen.Items.Add("Male");
            cmbShowGen.Items.Add("Female");
            cmbShowHours.Items.Add("Select");
            for (int i = 1; i <= 24; i++)
            {
                cmbShowHours.Items.Add(i);
            }
            cmbShowSec.SelectedIndex = -1;
            chkStatus.IsChecked = true; 
            #endregion

        }

        #region Automatic TaskID Generation
        public void GetTaskIdentity()
        {
            DataTable mydtFrTskIdentity = ORGTaskMasterDAL.GetDBDetails(5);
            if (mydtFrTskIdentity.Rows.Count > 0)
            {
                if (mydtFrTskIdentity.Rows[0][0].ToString() == "0" || mydtFrTskIdentity.Rows[0][0].ToString() == "")
                {
                    lblShowTskID.Content = 1.ToString();
                }
                else
                {
                    lblShowTskID.Content = (Int32.Parse(mydtFrTskIdentity.Rows[0][0].ToString()) + 1).ToString();
                }
            }
        } 
        #endregion

        #region Clear Form
        private void btnClear_Click(object sender, RoutedEventArgs e)
        {

            GetTaskIdentity();
            cmbShowGen.SelectedIndex = 0;
            cmbShowHours.SelectedIndex = 0;
            cmbShowSec.SelectedIndex = -1;
            txtTaskShowNm.Text = "";
            txtShowPPDay.Text = "";
            txtShowDesc.Text = "";
            chkStatus.IsChecked = true;

        } 
        #endregion

        #region Display Selected Section Details
        private void cmbShowSec_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbShowSec.SelectedIndex == -1)
            {
                return;
            }
            DataTable mydt = ORGTaskMasterDAL.GetDBDetails(7, ((DataRowView)cmbShowSec.Items[cmbShowSec.SelectedIndex]).Row[1].ToString());

            grdDisp.ItemsSource = mydt;

        } 
        #endregion

        #region Save Details
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {

            if (validation())
            {
                DataTable mydt = new DataTable();
                int isselect = 1;
                if (chkStatus.IsChecked == false)
                {
                    isselect = 0;
                }



                if (Mode == "")
                {
                    
                     mydt = ORGTaskMasterDAL.InsertAndGetDetails(Int32.Parse(((DataRowView)cmbShowSec.Items[cmbShowSec.SelectedIndex]).Row[0].ToString()), txtTaskShowNm.Text, cmbShowGen.Text + "_" + cmbShowHours.Text + "_" + txtShowPPDay.Text, txtShowDesc.Text, isselect, ((DataRowView)cmbShowSec.Items[cmbShowSec.SelectedIndex]).Row[1].ToString(), 8);
                    MessageBox.Show("Details Inserted Sucessfully", "Information", MessageBoxButton.OK, MessageBoxImage.Information);                   

                }
                if (Mode == "Update")
                {                    
                     mydt = ORGTaskMasterDAL.UpdateAndSelectDetails(Int32.Parse(lblShowTskID.Content.ToString()), Int32.Parse(((DataRowView)cmbShowSec.Items[cmbShowSec.SelectedIndex]).Row[0].ToString()), txtTaskShowNm.Text, cmbShowGen.Text + "_" + cmbShowHours.Text + "_" + txtShowPPDay.Text, txtShowDesc.Text, isselect, ((DataRowView)cmbShowSec.Items[cmbShowSec.SelectedIndex]).Row[1].ToString(), 11);
                    MessageBox.Show("Details Updated Sucessfully", "Information", MessageBoxButton.OK, MessageBoxImage.Information);                   
                    Mode = "";
                }
                grdDisp.ItemsSource = mydt;
                this.btnClear_Click(this, e);
            }
        } 
        #endregion

        #region  Validation
        public bool validation()
        {
            bool status = false;

            if (cmbShowSec.SelectedIndex == -1)
            {
                MessageBox.Show("Section Must be Selected", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                cmbShowSec.Focus();
                return false;
            }
            if (txtTaskShowNm.Text == "")
            {
                MessageBox.Show("Task Name is required", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                txtTaskShowNm.Focus();
                return false;
            }

            if (cmbShowGen.SelectedIndex == 0)
            {
                MessageBox.Show("Gender Must be Selected", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                cmbShowGen.Focus();
                return false;
            }
            if (cmbShowHours.SelectedIndex == 0)
            {
                MessageBox.Show("Hours Must be Selected", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                cmbShowHours.Focus();
                return false;
            }
            if (txtShowPPDay.Text == "")
            {
                MessageBox.Show("Pay per day should be mentioned", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                txtShowPPDay.Focus();
                return false;
            }
            if (txtShowPPDay.Text != "")
            {
                if (!Regex.Match(txtShowPPDay.Text, @"^[-+]?[0-9]*\.?[0-9]+$").Success)
                {
                    MessageBox.Show(" Please Enter Correct Amount", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
                    txtShowPPDay.Clear();
                    txtShowPPDay.Focus();
                    return false;
                }
            }
            return true;
        } 
        #endregion

        #region DatagridView Activated Row
        private void grdDisp_RowActivated(object sender, Telerik.Windows.Controls.GridView.RowEventArgs e)
        {
            Mode = "Update";
            if (grdDisp.SelectionMode == SelectionMode.Single)
            {
                DataTable mydt = ORGTaskMasterDAL.GetDBDetails(10, ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[0])).Value.ToString());
                for (int i = 0; i < mydt.Rows.Count; i++)
                {
                    if ((mydt.Rows[i][2].ToString() == (((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[1])).Value.ToString())))
                    {
                        lblShowTskID.Content = mydt.Rows[i][0].ToString();
                        cmbShowSec.Text = mydt.Rows[i][1].ToString();
                        txtTaskShowNm.Text = mydt.Rows[i][2].ToString();
                        string[] tskmtrx = (mydt.Rows[i][3].ToString()).Split('_');
                        cmbShowGen.Text = tskmtrx[0].ToString();
                        cmbShowHours.Text = tskmtrx[1].ToString();
                        txtShowPPDay.Text = tskmtrx[2].ToString();
                        txtShowDesc.Text = mydt.Rows[i][4].ToString();
                        if (mydt.Rows[i][5].ToString() == "1")
                        {
                            chkStatus.IsChecked = true;
                        }
                        else
                        {
                            chkStatus.IsChecked = false;
                        }
                    }
                }

            }

        } 
        #endregion

        #region Duplication Checking for TaskName and Task Matrix
        private void txtShowPPDay_LostFocus(object sender, RoutedEventArgs e)
        {
            bool status = false;
            if (cmbShowGen.SelectedIndex != 0 && cmbShowHours.SelectedIndex != 0 && txtShowPPDay.Text != "")
            {
                DataTable mydt = ORGTaskMasterDAL.GetDetailsforValidation(Int32.Parse(((DataRowView)cmbShowSec.Items[cmbShowSec.SelectedIndex]).Row[0].ToString()), 9);
                for (int i = 0; i < mydt.Rows.Count; i++)
                {
                    if (cmbShowGen.Text + "_" + cmbShowHours.Text + "_" + txtShowPPDay.Text == mydt.Rows[i][1].ToString())
                    {
                        status = true;
                    }
                }
                if (status == true)
                {
                    MessageBox.Show(cmbShowGen.Text + "_" + cmbShowHours.Text + "_" + txtShowPPDay.Text + " for " + cmbShowSec.Text + " is already utilized, please choose another Task Matrix ", "Duplicate Task Matrix", MessageBoxButton.OK, MessageBoxImage.Error);
                    cmbShowGen.SelectedIndex = 0;
                    cmbShowHours.SelectedIndex = 0;
                    txtShowPPDay.Text = "";
                    cmbShowGen.Focus();
                }
            }
        }

        private void txtTaskShowNm_LostFocus(object sender, RoutedEventArgs e)
        {
            bool flag = true;
            if (txtTaskShowNm.Text != "")
            {
                DataTable mydt = ORGTaskMasterDAL.GetDetailsforValidation(Int32.Parse(((DataRowView)cmbShowSec.Items[cmbShowSec.SelectedIndex]).Row[0].ToString()), 9);
                for (int i = 0; i < mydt.Rows.Count; i++)
                {
                    if (txtTaskShowNm.Text == mydt.Rows[i][0].ToString())
                    {
                        flag = false;
                    }
                }
            }
            if (flag == false)
            {
                MessageBox.Show(txtTaskShowNm.Text + " for " + cmbShowSec.Text + " is already utilized, please choose another Task Name ", "Duplicate Task Name", MessageBoxButton.OK, MessageBoxImage.Error);
                cmbShowGen.Focus();
                txtTaskShowNm.Clear();
            }
        } 
        #endregion

    }
}
